package com.bidevalution.business.impl;

import com.bidevalution.business.enums.VoteAlgorithmEnum;
import com.bidevalution.business.enums.VoteOptionEnum;
import com.bidevalution.business.enums.VoteTypeEnum;
import com.bidevalution.business.service.BeSectionExpertInfoService;
import com.bidevalution.business.service.VoteUnitNumService;
import com.bidevalution.business.vote.service.BeExpertVoteService;
import com.bidevalution.dao.entity.BeExpertVoteDetailExt;
import com.bidevalution.dao.entity.BeExpertVoteExt;
import com.bidevalution.dao.entity.BeSectionEnterprise;
import com.bidevalution.dao.entity.BeSectionExpertInfo;
import com.bidevalution.dao.mapper.BeBidResultMapper;
import com.bidevalution.dao.mapper.BeSectionEnterpriseMapper;
import com.qtp.core.util.GsonUtil;
import com.qtp.core.util.OConvertUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 处理业务功能
 * @Date 2020-04-16 13:47
 * @Author lhl
 */
@Log4j2
@Service
public class VoteUnitNumServiceImpl implements VoteUnitNumService {

    @Autowired(required = false)
    BeBidResultMapper beBidResultMapper;
    @Autowired(required = false)
    BeSectionEnterpriseMapper beSectionEnterpriseMapper;
    @Autowired
    BeExpertVoteService beExpertVoteService;
    @Autowired
    BeSectionExpertInfoService beSectionExpertInfoService;

    @Override
    public void checkInitiateVote(String sectionId, String sectionStepId, int warnUnitNum, int isProcessBefore) throws Exception {
        log.info("LOG[]checkInitiateVote[]{},params={}", "记录日志",GsonUtil.toJsonWithGson(sectionId)+";"+GsonUtil.toJsonWithGson(sectionStepId)+";"+GsonUtil.toJsonWithGson(warnUnitNum));
        if (isProcessBefore == 1){
            List<BeSectionEnterprise> beSectionEnterprises = beSectionEnterpriseMapper.selectBySectionId(sectionId);
            if (beSectionEnterprises.size() >= warnUnitNum){
                return;
            }
        }else {
            Map<String, Object> passedUnitNum = beBidResultMapper.getPassedUnitNum(sectionStepId);
            //通过的单位数
            int passed = OConvertUtils.getInt(passedUnitNum.get("passed"));
            //未通过的单位数
            int notPassed = OConvertUtils.getInt(passedUnitNum.get("notPassed"));
            //是否流程第一环节
            if (!(passed < warnUnitNum && notPassed > 0)){
                return;
            }
        }
        launchVote(sectionId,sectionStepId, warnUnitNum);
    }
    /**
     * @Description 发起表决
     * @param sectionId     标段id
     * @param warnUnitNum   单位基数
     * @date 2020-04-15 14:39
     * @author lhl
     */
    private void launchVote(String sectionId, String sectionStepId, int warnUnitNum) throws Exception {
        String title = String.format("小于%s家，仍有竞争力，继续评审", warnUnitNum);
        BeExpertVoteExt expertVote = new BeExpertVoteExt();
        List<BeExpertVoteExt> expertVoteList = new ArrayList<>();
        expertVote.setSectionId(sectionId);
        expertVote.setSectionStepId(sectionStepId);
        expertVote.setTitle(title);
        expertVote.setContent(title);
        expertVote.setVoteOptions(GsonUtil.toJson(VoteOptionEnum.getNames()));
        expertVote.setVoteType(VoteTypeEnum.Automatic.getCode());
        expertVote.setVoteAlgorithm(VoteAlgorithmEnum.ShaoShuFuCongDuoShu.code);
        expertVote.setOriginator("SYSTEM");
        expertVote.setNextJob("unitMinNumCallback");
        Map<String,Object> voteParams = new HashMap<String,Object>();
        voteParams.put("sectionId", sectionId);
        expertVote.setVoteParams(GsonUtil.toJson(voteParams));
        List<BeSectionExpertInfo> expertInfoList = beSectionExpertInfoService.getListBySectionId(sectionId, "0");
        List<BeExpertVoteDetailExt> voteDetailList = new ArrayList<>();
        BeExpertVoteDetailExt voteDetail;
        for (BeSectionExpertInfo expertInfo : expertInfoList) {
            voteDetail = new BeExpertVoteDetailExt();
            voteDetail.setExpertId(expertInfo.getSeqId());
            voteDetailList.add(voteDetail);
        }
        expertVote.setVoteDetailList(voteDetailList);
        expertVoteList.add(expertVote);
        beExpertVoteService.createVote(expertVoteList);
    }

}
